﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class ReaderViewForm : Form
    {
        public Reader reader = new Reader();
        bool add = false;
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet<Reader> readers;
        public static ObjectSet<Student> students;
        public static ObjectSet<Employee> employers;
        public static ObjectSet<Professor> professors;

        public ReaderViewForm(Reader reader, bool add)
        {
            this.reader = reader;
            this.add = add;
            readers = ins.Reader;
            students = ins.Student;
            employers = ins.Employee;
            professors = ins.Professor;
            InitializeComponent();
            CBType.Items.Add("Студент");
            CBType.Items.Add("Работник");
            CBType.Items.Add("Преподаватель");
            CBType.SelectedIndex = 0;
        }

       

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (CBType.SelectedIndex)
            {
                case 0: PanelStudent.Visible = true; PanelEmployee.Visible = false; PanelProffessor.Visible = false; break;
                case 1: PanelStudent.Visible = false; PanelEmployee.Visible = true; PanelProffessor.Visible = false; break;
                case 2: PanelStudent.Visible = false; PanelEmployee.Visible = false; PanelProffessor.Visible = true; break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ReaderViewForm_Load(object sender, EventArgs e)
        {
            if (reader != null)
            {
                TBFIO.Text = reader.FIO;
                TBPassport.Text = reader.Passport;
                TBReaderCard.Text = reader.ReadersCard;
                TBAdress.Text = reader.Address;
                TBHometel.Text = reader.TelHome;
                TBWorkTel.Text = reader.TelWork;
                CBType.SelectedIndex = reader.Category-1;
                switch (reader.Category)
                {
                    case 1:
                        {
                            PanelStudent.Visible = true;
                            TBFaculty.Text = reader.Student.Faculty;
                            TBGroup.Text = reader.Student.StudentGroup;
                            TBCourse.Text = reader.Student.Course;
                            break;
                        }
                    case 2:
                        {
                            PanelEmployee.Visible = true;
                            TBJob.Text = reader.Employee.Job;
                            TBPostEmployee.Text = reader.Employee.Post;
                            break;
                        }
                    case 3:
                        {
                            PanelProffessor.Visible = true;
                            TBDegree.Text = reader.Professor.AcademicDegree;
                            TBPost.Text = reader.Professor.Post;
                            TBDepartment.Text = reader.Professor.Department;
                            break;
                        }
                }

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            Employee employee = new Employee();
            Professor professor = new Professor();

            if (reader == null)
                reader = new Reader();
            else
                reader = readers.Where(x => x.ReaderId.Equals(reader.ReaderId)).FirstOrDefault();

            reader.FIO = TBFIO.Text;
            reader.Passport = TBPassport.Text;
            reader.ReadersCard = TBReaderCard.Text;
            reader.Address = TBAdress.Text;
            reader.TelHome = TBHometel.Text;
            reader.TelWork = TBWorkTel.Text;
           
            reader.Category = Convert.ToByte(CBType.SelectedIndex +1);

            if (add)
            {
                ins.AddToReader(reader);
            }
            ins.SaveChanges();

            switch (reader.Category)
            {
                case 1:
                    {
                        if (reader.Student == null)
                            student  = new Student();
                        else
                            student = students.Where(x => x.ReaderId.Equals(reader.ReaderId)).FirstOrDefault();

                        student.ReaderId = reader.ReaderId;
                        student.Faculty = TBFaculty.Text;
                        student.StudentGroup = TBGroup.Text;
                        student.Course = TBCourse.Text;
                       
                        break;
                    }
                case 2:
                    {

                        if (reader.Employee == null)
                            employee = new Employee();
                        else
                            employee = employers.Where(x => x.ReaderId.Equals(reader.ReaderId)).FirstOrDefault();

                        employee.ReaderId = reader.ReaderId;
                        employee.Job = TBJob.Text;
                        employee.Post = TBPostEmployee.Text;
                        break;
                    }
                case 3:
                    {
                        if (reader.Professor == null)
                            professor = new Professor();
                        else
                            professor = professors.Where(x => x.ReaderId.Equals(reader.ReaderId)).FirstOrDefault();

                        professor.ReaderId = reader.ReaderId;
                        professor.AcademicDegree = TBDegree.Text;
                        professor.Post = TBPost.Text;
                        professor.Department = TBDepartment.Text;
                        break;
                    }
            }


          
            switch (reader.Category)
            {
                case 1:
                    {
                        if (reader.Student == null)
                        {
                            ins.AddToStudent(student);
                            ins.SaveChanges();
                            student.ReaderId = reader.ReaderId;
                        }
                        break;
                    }
                case 2:
                    {
                        if (reader.Employee == null)
                        {
                            ins.AddToEmployee(employee);
                            ins.SaveChanges();
                            employee.ReaderId = reader.ReaderId;
                        }
                        break;
                    }
                case 3:
                    {
                        if (reader.Professor == null)
                        {
                            ins.AddToProfessor(professor);
                            ins.SaveChanges();
                            professor.ReaderId = reader.ReaderId;
                        }
                        break;
                    }

            }

            ins.SaveChanges();

            this.Close();
        }
    }
}
